package com.hudongwx.shansun.define;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.hudongwx.shansun.R;

/**
 * Created by hudongwx on 16-10-28.
 */
public class MyInputView extends FrameLayout {
    private ImageView iv;
    private EditText et;

    public MyInputView(Context context) {
        super(context);
        initView(context);
    }

    public MyInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyInputView);
        String text = typedArray.getString(R.styleable.MyInputView_text);
        int color = typedArray.getColor(R.styleable.MyInputView_color, Color.BLACK);
        int size = typedArray.getInteger(R.styleable.MyInputView_size, 15);
        et.setText(text);
        et.setTextSize(size);
        et.setTextColor(color);


    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.myinputview, this, true);
        iv = (ImageView) view.findViewById(R.id.myinput_iv);
        et = (EditText) view.findViewById(R.id.myinput_et);
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                et.setText("");
            }
        });
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    iv.setVisibility(View.GONE);
                } else {
                    iv.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

}
